package com.haidechizi.i18n.common.support;

import com.haidechizi.i18n.common.annotation.I18nField;
import com.haidechizi.i18n.common.utils.I18nFieldUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Map;

@Slf4j
public abstract class BaseI18nFieldMappingBinding implements I18nFieldMappingBinding {

    /**
     * 动态初始化I18nReflectUtils
     * 实现Field和I18nField的绑定关系
     */
    @Override
    public void binding() {
        Map<Field, I18nField> bindingData = getBindingData();
        log.info("自定义绑定关系,data:{}", bindingData);
        bindingData.forEach(((field, i18nField) -> {
            Class<?> clazz = field.getDeclaringClass();
            // 自定义属性
            if (I18nFieldUtils.isCustomField(i18nField)) {
                I18nReflectUtils.setCustomFieldsListWithAnnotation(clazz, field);
                // 组合属性
            } else if (I18nFieldUtils.isCombinedField(i18nField)) {
                I18nReflectUtils.setCombinedFieldsListWithAnnotation(clazz, field);
                // 简单属性
            } else {
                I18nReflectUtils.setSimpleFieldsListWithAnnotation(clazz, field);
            }
            I18nReflectUtils.setI18nFieldAnnotation(field, i18nField);
            I18nReflectUtils.setClassFieldHasInitMap(clazz, true);
        }));
    }

    /**
     * 设置Field和I18nField的绑定关系
     *
     * @return
     */
    public abstract Map<Field, I18nField> getBindingData();

    protected I18nField getCustomField(String metaCalculator) {
        return getI18nField("", "", "", "", metaCalculator);
    }

    protected I18nField CombinedField(final String value, final String systemCode, final String businessCode,
                                      final String businessKey) {
        return getI18nField(value, systemCode, businessCode, businessKey, "");
    }

    protected I18nField getSimpleField(String value) {
        return getI18nField(value, "", "", "", "");
    }

    protected I18nField getI18nField(final String value, final String systemCode, final String businessCode,
                                     final String businessKey, final String metaCalculator) {
        I18nField i18nField = new I18nField() {

            @Override
            public String value() {
                return value;
            }

            @Override
            public String systemCode() {
                return systemCode;
            }

            @Override
            public String businessCode() {
                return businessCode;
            }

            @Override
            public String businessKey() {
                return businessKey;
            }

            @Override
            public String metaCalculator() {
                return metaCalculator;
            }

            @Override
            public Class<? extends Annotation> annotationType() {
                return I18nField.class;
            }
        };
        return i18nField;
    }
}
